<?php
class Modular {
	function Modular() {
		
	}
	
	function init_controller() {
		require_once("controller.php");
		global $module_folder,$default_module,$default_controller;
		foreach($_GET as $name => $value) {  // $_GET['omrah/index/method/param1/param2/param3/../paramn']= '' -- www.oo.com/index.php?omrah/m/d&hajj=saleh
			$module_path = explode('/',$name);
			$files = $this->get_modules();
			
			$found = false;
			foreach($files as $file) {
				if($file == $module_path[0] && is_dir($module_folder."/".$file)) {
					$found = true;
					break;
				}
			}
			if(!$found) {
				break;
			}
		}
		$module = $module_path[0];
		//print_r($module_path);
		
		if(isset($module_path[1]) && is_file($module_folder.'/'.$module.'/controllers/'.$module_path[1].'.php')) {
			$controller = $module_path[1];
			$this->load_module($module,$controller);
			if(isset($module_path[2]))
				eval("\$this->module->control->$module_path[2]();");
			else 
				$this->module->control->execute();
		} else if(isset($module) && is_file($module_folder.'/'.$module.'/controllers/index.php')){
			$this->load_module($module,'index');
			$this->module->control->execute();
		} else {
			die("404");
		}
		
	}
	
	function get_modules() {
		global $module_folder;
		$handle = opendir($module_folder);
		while (false !== ($file = readdir($handle))) {
			if($file != '.' && $file != '..')
				$files[] = $file;
		}
		closedir($handle);
		return $files;
	}
	
	function load_module($module_name,$controller_name) {
		global $module_folder;
		require_once($module_folder."/".$module_name."/controllers/".$controller_name.".php");
		
		eval("\$this->module->control = new $controller_name();");
	}
}
?>